/**
 * Description:
 * Class Name:RandomFileChannelTest
 * Date:2022/5/3 18:14
 *
 * @author LTisme
 * @version JAVA17
 */
package 疯狂Java讲义.第15章_输入输出._15_9_NIO._15_9_3_使用Channel;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class RandomFileChannelTest {
    public static void main(String[] args) throws IOException {
        var f = new File("Chapter_15_9_3_randomFile.txt");
        try (
                var raf = new RandomAccessFile(f, "rw");
                FileChannel randomChannel = raf.getChannel()
                ){
            // 将Channel中的所有数据映射成ByteBuffer
            ByteBuffer buffer = randomChannel.map(FileChannel.MapMode.READ_ONLY, 0, f.length());
            // 把Channel的记录指针移动到最后
            randomChannel.position(f.length());
            randomChannel.write(buffer);
        }
    }
}
